#include "paramBuffer_t.h"
#include "../libcbgl32memory/libcbgl32memory.h"
#include <assert.h>

INLINE paramBuffer_t* paramBuffer_create()
{
	paramBuffer_t* rv;
	rv = (paramBuffer_t*)cbgl_malloc( sizeof(paramBuffer_t) );
	rv->available = MAX_BYTE_PER_BLOCK;
	rv->next = 0;
	return rv;
}

INLINE void* paramBuffer_addParams(paramBuffer_t* buf, size_t n)
{
	size_t offset;

	assert(buf);
	while( (buf->available <= n) && buf->next)
	{
		buf = buf->next;
	}

	if(buf->available <= n)
	{
		buf->next = paramBuffer_create();
		buf = buf->next;
	}

	offset = MAX_BYTE_PER_BLOCK - buf->available;
	buf->available -= n;
	return buf->buffer+offset;
}

INLINE void paramBuffer_destroy(paramBuffer_t* buf)
{
	paramBuffer_t* tmp;

	while(buf)
	{
		tmp = buf;
		buf = buf->next;
		cbgl_free(tmp);
	}
}

INLINE void paramBuffer_reset(paramBuffer_t* buf)
{
	assert(buf);

	while(buf)
	{
		buf->available = MAX_BYTE_PER_BLOCK;
		buf = buf->next;
	}

	
}